package com.etc.admin.controller;/*
 * 文件名：com.etc.admin.controller
 * 描述：
 * 作者：袁祥辉
 * 时间：2019/5/27 9:57
 */

import com.etc.entity.Comment;
import com.etc.entity.Goods;
import com.etc.entity.GoodsPicture;
import com.etc.service.GoodsPictureService;
import com.etc.service.GoodsService;
import com.etc.utils.CommonMessage;
import com.etc.utils.PageData;
import org.apache.ibatis.annotations.Param;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "admin.action")
public class GoodsPictureController {

    @Autowired
     private GoodsPictureService goodsPictureService;
    @Autowired
    private GoodsService goodsService;


    @GetMapping(value = "listGoodsPicture")
    @ResponseBody
    public PageData<GoodsPicture> listGoodsPicture(Integer page, Integer limit, String content){
        if (content == null){
            content="";
        }
        PageData<GoodsPicture> pd = goodsPictureService.getGoodsPictureByPage(page,limit,content);

        //返回Json格式的对象
        pd.setCode(0);
        pd.setMsg("请求成功");
        return pd;
    }
    @RequestMapping(value = "deleteGoodsPicture/{pictureid}",method = RequestMethod.DELETE)
    @ResponseBody
    public CommonMessage deleteGoodsPicture(@PathVariable("pictureid") Integer pictureid){
        Boolean flag = goodsPictureService.deleteGoodsPicturetById(pictureid);

        CommonMessage cm = new CommonMessage();
        if (flag){
            cm.setMsg("删除成功");
        }else{
            cm.setMsg("删除失败");
        }
        return cm;
    }

    /**

     * 添加图片
     * @return 返回到添加图片的jsp界面
     */
    @RequestMapping("addPicture")
    public ModelAndView addPicture(){
       ModelAndView mv=new ModelAndView("admin/addPicture");
       //得到还没有上传过图片的商品信息
        List<Goods> list=goodsService.listGoods();
        mv.addObject("list",list);

        return mv;
    }

    /**
     *
     * @param request
     * @param file 图片上传过来的文件
     * @return
     * @throws IOException 异常处理
     */
    @PostMapping(value = "uploadPicture")
    @ResponseBody
    public String uploadPic( HttpServletRequest request, @Param("file") MultipartFile file) throws IOException {
        //本地存储文件路径
        String rootPath = "D:\\imgs";
        //源文件名字
        String originalFilename = file.getOriginalFilename();
        //新的图片名称
        File newDir = new File(rootPath);
        //新文件
        File newFile = new File(rootPath + "/" + originalFilename);
        //判断目标文件所在的目录是否存在
        if (!newFile.getParentFile().exists()) {
            //如果目标文件所在的目录不存在，则创建父目录
            newFile.getParentFile().mkdirs();
        }
        //将内存中的数据写入磁盘
        file.transferTo(newFile);
        //完整的url
        String picUrl = "/imgs/" + originalFilename;
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map2 = new HashMap<String, Object>();
        map.put("code", 0);//0表示成功，1失败
        map.put("msg", "上传成功");//提示消息
        map.put("data", map2);
        map2.put("src", picUrl);//图片url
        map2.put("title", originalFilename);//图片名称，这个会显示在输入框里
        String result = new JSONObject(map).toString();
        return result;
    }

    /**
     *
     * @param goodsid 商品id
     * @param cover 封面路径
     * @param picture1 商品图片1路径editGoodsPicture
     * @param picture2 商品图片2路径
     * @return 返回是否成功还是失败
     */
    @RequestMapping("toAddPicture")
    @ResponseBody
    public CommonMessage addGoods(Integer goodsid,String cover,String picture1,String picture2){
        CommonMessage commonMessage=new CommonMessage();
        //创建三个图片对象，增加三张图片
        GoodsPicture gp1=new GoodsPicture(null,goodsid,cover,0);
        GoodsPicture gp2=new GoodsPicture(null,goodsid,picture1,1);
        GoodsPicture gp3=new GoodsPicture(null,goodsid,picture2,1);
        boolean flag1=goodsPictureService.addGoodsPicture(gp1);
        boolean flag2=goodsPictureService.addGoodsPicture(gp2);
        boolean flag3=goodsPictureService.addGoodsPicture(gp3);
        if(flag1 && flag2 && flag3){
            commonMessage.setMsg("添加成功");
        }else{
            commonMessage.setMsg("添加失败");
        }
        return commonMessage;
    }

    /**
     *
     * @param pictureid 图片id
     * @param picuture 修改后的图片地址
     * @return
     */
    @RequestMapping("editGoodsPicture")
    @ResponseBody
    public CommonMessage editGoodsPicture(Integer pictureid,String picuture){
        CommonMessage commonMessage=new CommonMessage();
        boolean flag=goodsPictureService.updateByPictureid(pictureid,picuture);
       if(flag){
            commonMessage.setMsg("修改成功");
        }else{
            commonMessage.setMsg("修改失败");
        }
        return commonMessage;
    }

    /**
     *根据商品id批量删除此商品的图片
     * @param goodsid 商品id
     * @return
     */
    @RequestMapping("delGoodsPicture")
    @ResponseBody
    public CommonMessage delGoodsPicture(Integer goodsid){
        System.out.println(goodsid);
        CommonMessage commonMessage=new CommonMessage();
        boolean flag=goodsPictureService.delGoodsPictureByGid(goodsid);
        if(flag){
            commonMessage.setMsg("删除成功");
        }else{
            commonMessage.setMsg("删除失败");
        }
        return commonMessage;
    }

}
